<template>
	<view class="upload_box" >
			<u-upload
					:fileList="fileList"
					:deletable="deletable"
					@afterRead="afterRead"
					@delete="deletePic"
					@click
					:source-type="sourceType"
					:name="name"
					multiple
					:previewImage='previewImage'
					:maxCount="maxCount"
					:disabled="disabled"
					ref="upload"
				>
				</u-upload>
	</view>
</template>

<script>
	import systemConfig from '@/api/systemConfig.js'
	export default {
		name:"upload",
		data() {
			return {
				imgList:[],
				fileList:[],
			};
		},
		props:{
			sourceType:{
				type:Array,
				default:['album', 'camera']
			},
			maxCount:{
				type:Number,
				default:10
			},
			name:{
				type:String,
				require:true
			},
			deletable:{
				type:Boolean,
				default:true
			},
			isreLoad:{
				type:Boolean,
				default:false
			},
			fileListInit:{
				 type: Array,
				      // default: function () { return [] }
				default: () => []
			},
			disabled:{
				type:Boolean,
				default:false
			},
			previewImage:{
				type:Boolean,
				default:true
			},
		},
		created() {
			setTimeout(()=>{
				if(this.fileListInit.length>0){
					if(this.fileListInit[0]!=''){
						this.imgList=this.fileListInit
						this.fileList=this.fileListInit.map(url=>{
							return {
								url:'https://'+url
							}
							
						})
					}
					
				}
			},1000)
			
		},
		methods:{
			clickUpload(){
				this.$refs['upload'].chooseFile()
			},
			reLoad(){
				if(this.imgList.length>=2){
					let e={index:0}
					this.deletePic(e)
				}
			},
			// 删除图片
			deletePic(event) {
				this.fileList.splice(event.index, 1)
				this.imgList.splice(event.index, 1)
				this.$emit('uploadFile',{files:this.imgList,name:this.name})
			},
			// 新增图片
			async afterRead(event) {
                console.log('event: ',event);				
				this.$emit('uploadFile',{files:this.imgList,name:this.name})
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				console.log('event.file',event.file);
				let lists = [].concat(event.file)
				console.log(event, event.file,event.name)
				let fileListLen = this.fileList.length
				lists.map((item) => {
					this.fileList.push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this.fileList[fileListLen]
					this.fileList.splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: systemConfig.apiImg, // 仅为示例，非真实的接口地址
						filePath: url,
						formData: {
							user: 'test'
						},
						name: 'file',
						header: {
							'token': uni.getStorageSync('token')
						},
						success: (res) => {
							console.log('res', res);
							console.log('this.imgList1',this.imgList);
							this.imgList = [...this.imgList, ...JSON.parse(res.data).result]
							console.log(res,'图片上传完成')
							console.log('this.imgList2',this.imgList)
							if(this.isreLoad&&this.maxCount==1){
								this.reLoad()
							}
							this.$emit('uploadFile',{files:this.imgList,name:this.name})
							setTimeout(() => {
								resolve(res.data.data)
							}, 1000)
						}
					});
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
::v-deep.upload_box{
	display: flex;
	align-items: center;
	.u-upload{
	}
	.u-upload__deletable{
		z-index: 1;
	}
}
</style>